package com.dbz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dbz.entity.Journal;
import com.dbz.entity.Product;
import com.dbz.entity.ProductType;
import com.dbz.service.ProductService;
import com.dbz.service.ProductTypeService;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@RestController
@RequestMapping("/productType/")
public class ProductTypeController {
    @Resource
    private ProductTypeService productTypeService;
    @Resource
    private ProductService productService;

    @RequestMapping("getProductTypeAll")
    public List<ProductType> getProductTypeAll() {
        return productTypeService.getProductTypeAll();
    }

    /**
     * 分页查询
     * @param protypename
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectPage")
    public Map pageList(String protypename, Integer page, Integer rows) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<ProductType>();
        queryWrapper.like(protypename != "" && protypename != null, "protypename", protypename);
        IPage<ProductType> pages = new Page<ProductType>(page, rows);
        map.put("rows", productTypeService.selectPage(pages, queryWrapper).getRecords());
        map.put("total", productTypeService.selectPage(pages, queryWrapper).getTotal());
        return map;
    }

    /**
     * 添加分类信息
     * @param protypename
     * @return
     */
    @RequestMapping("insertProType")
    public int insertProType(String protypename){
        return productTypeService.insertProType(protypename);
    }
    /**
     * 修改分类信息
     * @param protypename
     * @return
     */
    @RequestMapping("updateProType")
    public int updateProType(Integer protypeid,String protypename){
        return productTypeService.updateProType(protypeid,protypename);
    }
    /**
     * 删除分类信息
     * @param protypeid
     * @return
     */
    @RequestMapping("deleteProType")
    public int deleteProType(Integer protypeid){
        return productTypeService.deleteProType(protypeid);
    }

    /**
     * 统计管理
     * @return
     */
    @RequestMapping("productList")
    public Map<String, Object> productList() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<String> nameList = new ArrayList<String>();// 存放名称，x轴
        List<Integer> dataList = new ArrayList<Integer>();// 存放数量Y轴
        List<ProductType> productTypeList = productTypeService.getProductTypeAll();//查询所有类别
        for (ProductType productType : productTypeList) {
            nameList.add(productType.getText());
            List<Product> productList = productService.selectProByProTypeId(productType.getId());//根据id查询数量
            dataList.add(productList.size());
        }
        map.put("nameList", nameList);
        map.put("data", dataList);
        return map;
    }

        @RequestMapping("selectproType")
    public Set<ProductType> selectproType(){
        return productTypeService.selectproType();

    }
}

